﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ZLDC.Common.Estate;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace ZLDC.Topo.BLL.Estate
{
    public class SystemParamBLL
    {
                                                                public static DataTable List(int page, int pagesize, string condition)
        {
            DataTable dt = null;
            int total = 0;
    
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.SystemParamDAL.p_system_param_list_table(page, pagesize, condition, ref total, db);
            }

            return dt;
        }

                                                public static DataTable ListByType(string type)
        {
            DataTable dt = null;
            int total = 0;

                        object obj = _Cache.GetCache(type);
            if (obj != null)
            {
                dt = (DataTable)obj;
            }
            else
            {
                string condition = " where type='" + type + "'";
                using (_Database db = new _Database("estate"))
                {
                    dt = ZLDC.DAL.Estate.SystemParamDAL.p_system_param_list_table(1, 50, condition, ref total, db);
                }

                _Cache.SetCache(type, dt, 86400);
            }

            return dt;
        }

                                                public static DataTable ListUsage(string type)
        {
            DataTable dt = ListByType(type);    
                        int deptid = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID, 0);
            
            ZLDC.Topo.Model.Estate.AuthorizationInfo info = ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_detail(deptid);
            string usage = "";
            if (info != null)
            {
                if (info.Useage != null && info.Useage != "")
                {
                                        string lastChar = info.Useage.Substring(info.Useage.Length - 1, 1);
                    if (lastChar.Contains(","))
                        usage = info.Useage.Substring(0, info.Useage.Length - 1);
                    else
                        usage = info.Useage;
                }
                else
                {
                    usage = "1";
                }
            }

            DataRow[] dr = dt.Select("mark3 in(" + usage + ")");

            dt = _Utility.GetTable(dr);

            return dt;
        }

                                                public static string SelectItemByType(string type)
        {
            DataTable dt = null;
            int total = 0;

                        object obj = _Cache.GetCache(type);
            if (obj != null)
            {
                dt = (DataTable)obj;
            }
            else
            {
                string condition = " where type='" + type + "'";
                using (_Database db = new _Database("estate"))
                {
                    dt = ZLDC.DAL.Estate.SystemParamDAL.p_system_param_list_table(1, 50, condition, ref total, db);
                }

                _Cache.SetCache(type, dt, 86400);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

                                                        public static string SelectItemByType(string type, string itemvalue)
        {
            DataTable dt = null;
            int total = 0;
            string condition = " where type='" + type + "'";

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.SystemParamDAL.p_system_param_list_table(1, 50, condition, ref total, db);
            }

            _Json json = new _Json(dt);

            return "{record:" + json.ToJson(itemvalue) + "}";
        }

                                                                public static string SystemParamList(int page, int pagesize, string condition)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.SystemParamDAL.p_system_param_list_table(page, pagesize, condition, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }

                                                public static string SystemParamDelete(int id)
        {
            string outmsg = "";

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.SystemParamDAL.p_system_param_delete(id, out outmsg, db);
            }

            return outmsg;
        }

                                        public static string SystemParamSave(int ID, string type, string item, string mark3, string mark2, string mark1)
        {
            string outmsg = "";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.SystemParamDAL.p_system_param_save(ID, type, item, mark3, mark2, mark1, out outmsg, out newid, db);
            }

            return outmsg;
        }

                                                public static string SystemParamDetail(int id)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.SystemParamDAL.p_system_param_detail_table(id, db);
            }

            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

                                        public static void LoadParamType(HtmlSelect sType, bool isAddItem)
        {
            using (_Database db = new _Database("estate"))
            {
                DataTable dt = ZLDC.DAL.Estate.SystemParamDAL.SelectSystemParamType(db);
                sType.DataSource = dt;
                sType.DataTextField = "type";
                sType.DataValueField = "type";
                sType.DataBind();

                if (isAddItem)
                {
                    ListItem item = new ListItem();
                    item.Text = "手填";
                    item.Value = "手填";
                    sType.Items.Insert(0, item);
                }
            }
        }

                                                        public static void LoadParamType(HtmlSelect sType, bool isAddItem, string type)
        {
            DataTable dt = ListByType(type);
            sType.DataSource = dt;
            sType.DataTextField = "item";
            sType.DataValueField = "item";
            sType.DataBind();

            if (isAddItem)
            {
                ListItem item = new ListItem();
                item.Text = "全部";
                item.Value = "";
                sType.Items.Insert(0, item);
            }
        }

                                                        public static void LoadUsage(HtmlSelect sType, bool isAddItem, string type)
        {
            DataTable dt = ListUsage(type);
            if (dt != null)
            {
                sType.DataSource = dt;
                sType.DataTextField = "item";
                sType.DataValueField = "mark3";
                sType.DataBind();
            }
            if (isAddItem)
            {
                ListItem item = new ListItem();
                item.Text = "全部";
                item.Value = "";
                sType.Items.Insert(0, item);
            }
        }

                                                        public static void LoadUsageAll(HtmlSelect sType, bool isAddItem, string type)
        {
            DataTable dt = ListByType(type);
            sType.DataSource = dt;
            sType.DataTextField = "item";
            sType.DataValueField = "mark3";
            sType.DataBind();

            if (isAddItem)
            {
                ListItem item = new ListItem();
                item.Text = "全部";
                item.Value = "";
                sType.Items.Insert(0, item);
            }
        }

                                                        public static void LoadUsage(DropDownList sType, bool isAddItem, string type)
        {
            DataTable dt = ListUsage(type);
            sType.DataSource = dt;
            sType.DataTextField = "item";
            sType.DataValueField = "mark3";
            sType.DataBind();

            if (isAddItem)
            {
                ListItem item = new ListItem();
                item.Text = "无";
                item.Value = "";
                sType.Items.Insert(0, item);
            }
        }

                                                        public static void LoadUsageAll(DropDownList sType, bool isAddItem, string type)
        {
            DataTable dt = ListByType(type);
            sType.DataSource = dt;
            sType.DataTextField = "item";
            sType.DataValueField = "mark3";
            sType.DataBind();

            if (isAddItem)
            {
                ListItem item = new ListItem();
                item.Text = "全部";
                item.Value = "";
                sType.Items.Insert(0, item);
            }
        }

                                                        public static void LoadParamType1(HtmlSelect sType, bool isAddItem, string type)
        {
            DataTable dt = ListByType(type);
            sType.DataSource = dt;
            sType.DataTextField = "item";
            sType.DataValueField = "item";
            sType.DataBind();

            if (isAddItem)
            {
                ListItem item = new ListItem();
                item.Text = "请选择";
                item.Value = "";
                sType.Items.Insert(0, item);
            }
        }

        public static void LoadIDParamType(HtmlSelect sType, bool isAddItem, string type)
        {
            DataTable dt = ListByType(type);
            sType.DataSource = dt;
            sType.DataTextField = "item";
            sType.DataValueField = "mark3";
            sType.DataBind();

            if (isAddItem)
            {
                ListItem item = new ListItem();
                item.Text = "请选择";
                item.Value = "";
                sType.Items.Insert(0, item);
            }
        }
        
        public static void LoadMarkParamType(HtmlSelect sType, bool isAddItem, string type)
        {
            DataTable dt = ListByType(type);
            sType.DataSource = dt;
            sType.DataTextField = "item";
            sType.DataValueField = "mark3";
            sType.DataBind();

            if (isAddItem)
            {
                ListItem item = new ListItem();
                item.Text = "请选择";
                item.Value = "";
                sType.Items.Insert(0, item);
            }
        }
        
                                                        public static void LoadParamType(DropDownList sType, bool isAddItem, string type)
        {
            DataTable dt = ListByType(type);
            sType.DataSource = dt;
            sType.DataTextField = "item";
            sType.DataValueField = "item";
            sType.DataBind();

            if (isAddItem)
            {
                ListItem item = new ListItem();
                item.Text = "无";
                item.Value = "";
                sType.Items.Insert(0, item);
            }
        }

                                                public static string ParamTypeByJson(string type)
        {
            DataTable dt = ListByType(type);
 
            _Json json = new _Json(dt);
            return "{record:" + json.ToJson() + "}";
        }

    }
}
